from loguru import logger

from Configs import mongodb_live, mongodb_subject, mongodb_video
from DB import mongo_client



bool_live,data_live = mongo_client.find_document(mongodb_live, {"second_class" : {"$exists":True}}, Type=2)
projection = {
    "_id": 0,
    "SubjectId": 1,
    "SubjectName": 1,
    "SubjectLink": 1,
    "SubjectIntroduction": 1,
    "SubjectBanner": 1,
    "Organizers": 1,
    "Undertaker": 1,
}
bool_sub,data_sub = mongo_client.find_document(mongodb_subject, {"Source":"蔻享专题"},projection=projection, Type=2)
sub_list = [sub for sub in data_sub]

logger.debug("直播已经开始聚合")
res_data = []
for live in data_live:
    SubjectId = live.get("SubjectId")
    if not SubjectId:
        res_data.append(live)
        continue
    else:
        for sub in sub_list:
            if SubjectId == sub["SubjectId"]:
                live["SubjectName"] = sub["SubjectName"]
                live["SubjectLink"] = sub["SubjectLink"]
                live["SubjectIntroduction"] = sub["SubjectIntroduction"]
                live["SubjectBanner"] = sub["SubjectBanner"]
                live["Organizers"] = sub["Organizers"]
                live["Undertaker"] = sub["Undertaker"]
                res_data.append(live)

mongo_client.insert_document("huiyiyun_live_relevance", res_data, Type=2)
logger.debug("直播数据聚合完成")

logger.debug("视频已经开始聚合")
video_res_data = []
bool_video,data_video = mongo_client.find_document(mongodb_video,{"$and":[{"second_class": {"$exists": True}}]} , Type=2)
for video in data_video:
    SubjectId = video.get("SubjectId")
    Source = video.get("Source")
    if not SubjectId or Source == "知领专题":
        video_res_data.append(video)
        continue
    else:
        for sub in sub_list:
            if SubjectId == sub["SubjectId"]:
                video["SubjectName"] = sub["SubjectName"]
                video["SubjectLink"] = sub["SubjectLink"]
                video["SubjectIntroduction"] = sub["SubjectIntroduction"]
                video["SubjectBanner"] = sub["SubjectBanner"]
                video["Organizers"] = sub["Organizers"]
                video["Undertaker"] = sub["Undertaker"]
                video_res_data.append(video)

mongo_client.insert_document("huiyiyun_video_relevance", video_res_data, Type=2)
logger.debug("视频数据聚合完成")

